import {
  Controller,
  Post,
  UseInterceptors,
  UploadedFile,
  HttpException,
  HttpStatus,
  UploadedFiles,
} from '@nestjs/common'
import { FileService } from './file.service'
import { FileInterceptor, FilesInterceptor } from '@nestjs/platform-express'

import multer = require('multer')

@Controller('file')
export class FileController {
  constructor(private readonly fileService: FileService) {}

  @Post('uploads')
  @UseInterceptors(
    FilesInterceptor('file', 10, {
      storage: multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, 'D:/oss/image')
        },
        filename: (req, file, cb) => {
          cb(null, file.originalname)
        },
      }),
    }),
  )
  async uploadFile(@UploadedFiles() files: Array<Express.Multer.File>) {
    const filesArr: string[] = []
    for (const file of files) {
      filesArr.push(await this.fileService.uploadImage(file))
    }
    return filesArr
  }
  @Post('upload')
  @UseInterceptors(
    FileInterceptor('file', {
      storage: multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, 'D:/oss/image')
        },
        filename: (req, file, cb) => {
          cb(null, file.originalname)
        },
      }),
    }),
  )
  async upload(@UploadedFile() file: any): Promise<any> {
    return await this.fileService.uploadImage(file)
  }
}
